package com.jdls.tanhua.dubbo.impl;

import com.jdls.tanhua.dubbo.MovementTimeLineApi;
import com.jdls.tanhua.dubbo.utils.TimeLineService;
import com.jdls.tanhua.mongo.Friend;
import com.jdls.tanhua.mongo.MovementTimeLine;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author 嘉德罗斯
 * @date 2021/10/27
 */
@DubboService
public class MovementTimeLineImpl implements MovementTimeLineApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private TimeLineService timeLineService;

    @Override

    public void save(ObjectId id, List<Friend> friends) {
        timeLineService.save(id,friends);
    }

    @Override
    public List<MovementTimeLine> findAll(List<Long> ids,Long userId) {

        Query query = Query.query(Criteria.where("userId").in(ids).and("friendId").is(userId))
                .with(Sort.by(Sort.Order.desc("created")));

        List<MovementTimeLine> movementTimeLines = mongoTemplate.find(query, MovementTimeLine.class);
        return movementTimeLines;

    }

}
